<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <div class="wrap">
        <p>用户名：<input type="text" id="user"></p>
        <p>密&emsp;码：<input type="text" id="pwd"></p>
        <p><input id="btn" type="submit"></p>
    </div>
</body>
<script>

    var userInp = document.querySelector("#user");
    var pwdInp = document.querySelector("#pwd");
    var btn = document.querySelector("#btn");



    // var user = getCookie("user");
    // console.log(user);
    // if (user) {
    //     userInp.value = user;
    //     pwdInp.value = getCookie("pwd");
    // }

    // var count = 0;
    // 多用户注册
    btn.onclick = function () {
        var user = userInp.value;
        var pwd = pwdInp.value;
        if (getCookie("user_" + user)) {
            if (pwd == getCookie("pwd_" + user)) {
                location.href = "01.html";
            } else {
                alert("用户名或密码错误");
            }
        } else {
            alert("用户名不存在");
        }

    }


    function setCookie(name, val, day, path) {
        var date = new Date();
        path = path || "/";
        date.setDate(date.getDate() + day);
        document.cookie = name + "=" + encodeURIComponent(val); + ";expires=" + date + ";path=" + path;
    }

    function getCookie(name) {
        var cookie = document.cookie;
        if (cookie) {
            // var dataArr = cookie.split("; ");// "; "分隔
            var dataArr = cookie.split(";");    //  ";"分隔用trim()去空格
            for (var i = 0; i < dataArr.length; i++) {
                var item = dataArr[i].trim();
                // console.log(item);
                var key = item.split("=")[0];
                var value = item.split("=")[1];
                // console.log(key, value);
                if (key == name) {
                    return decodeURIComponent(value);
                }

            }
        }
        return "";
    }
</script>

</html>